package com.hyfd.task;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.hyfd.common.utils.DateTimeUtils;
import com.hyfd.common.utils.MD5;
import com.hyfd.common.utils.ToolHttp;
import com.hyfd.common.utils.XmlUtils;
import com.hyfd.dao.mp.OrderDao;
import com.hyfd.dao.mp.ProviderPhysicalChannelDao;
import com.hyfd.rabbitMq.RabbitMqProducer;
import com.hyfd.rabbitMq.SerializeUtil;

@Component
public class WFZhitongFlowTask {

Logger log = Logger.getLogger(getClass());
	
	@Autowired
	OrderDao orderDao;
	@Autowired
	RabbitMqProducer mqProducer;//消息队列生产者
	@Autowired
	ProviderPhysicalChannelDao providerPhysicalChannelDao;//物理通道信息
	
//	@Scheduled(fixedDelay = 60000)
	public void queryZhiTongOrder(){
		Map<String,Object> map = new HashMap<String,Object>();
		int flag = 2;
		try {
			String providerId = "0000000068";//
			Map<String,Object> channel = providerPhysicalChannelDao.selectByProviderId(providerId);//获取通道
			String defaultParameter = (String) channel.get("default_parameter");//参数
			Map<String,String> paramMap = XmlUtils.readXmlToMap(defaultParameter);
			String linkUrl = (String) channel.get("link_url");//充值地址
			String action = "queryReport";//操作（固定值Charge）
			String ver = "1.1";
			String account = paramMap.get("account");//平台申请的独立API账号
			String apiKey = paramMap.get("apiKey");//平台的appKey
			//查询订单中属于指通的折扣
			Map<String,Object> param = new HashMap<String,Object>();
			param.put("dispatcherProviderId", providerId);
			param.put("status", "1");
			List<Map<String,Object>> orderList = orderDao.selectByTask(param);//查询出所有尚通的充值中的订单
			for(int i = 0 , size = orderList.size() ; i < size ; i ++){
				Map<String,Object> order = orderList.get(i);
				String orderId = order.get("orderId")+"";//平台订单号
				
				map.put("orderId", orderId);
				String md5Str = "account=" + account + "&key=" + apiKey;
				String sign = MD5.ToMD5(md5Str);
				String paramStr = "v="+ver+"&action="+action+"&account="+account+"&OutTradeNo="+orderId+"&SendTime="+DateTimeUtils.formatDate(new Date(), "yyyy-MM-dd")+"&sign="+sign;
				String result = ToolHttp.post(false, linkUrl, paramStr, "application/x-www-form-urlencoded");
				//判断是否成功
				if(result!=null && !result.equals("")){
					JSONObject resultJson = JSONObject.parseObject(result);//将返回值转为json对象
					String code = resultJson.getString("Code");//返回状态码
					String message = resultJson.getString("message");//返回信息
					JSONArray reports = resultJson.getJSONArray("Reports");
					JSONObject report = reports.getJSONObject(0);
					String status = report.getString("Status");//充值状态
					map.put("resultCode", status + ":" + message);
					if(code.equals("0")){
						if(status.equals("4")){
							flag = 1;
						}else if(status.equals("5")||status.equals("3")){
							flag = 0;
						}else if(status.equals("2")||status.equals("7")){
							continue;
						}
					}
				}
				map.put("status", flag);
				mqProducer.sendDataToQueue(RabbitMqProducer.Result_QueueKey, SerializeUtil.getStrFromObj(map));
			}
		} catch (Exception e) {
			log.error("指通流量查询出错"+e);
		}
	}
}
